home *** CD-ROM | disk | FTP | other *** search
- Path: faw.uni-ulm.de!peinel
- From: peinel@faw.uni-ulm.de (Gertraud Peinel)
- Newsgroups: comp.lang.c
- Subject: Re: malloc question
- Date: 12 Mar 1996 15:24:36 GMT
- Organization: FAW Ulm
- Distribution: world
- Message-ID: <4i44vk$g16@rigel.rz.uni-ulm.de>
- References: <4htonk$350@news.hklink.net> <danpop.826488975@rscernix>
- NNTP-Posting-Host: merlin.faw.uni-ulm.de
-
- In article <danpop.826488975@rscernix>, danpop@mail.cern.ch (Dan Pop) writes:
- |> In <4htonk$350@news.hklink.net> alex@station.net (Alex Chu) writes:
- |>
- |> >typedef struct item {
- |> > int val;
- |> > struct item *next;
- |> >} ITEM, *PITEM;
- |> >
- |> >main()
- |> >{
- |> > PITEM head, current;
- |> > head=(PITEM) malloc(sizeof(ITEM));
- |> > ^^^^^^^
- |> > head->val=1;
- |> >}
- |> >
- |> >I want to know why need to use the type casting PITEM in front of the
- |> >malloc ? Please help!
- |>
- |> You DON'T have to cast the value returned by malloc, casting it is actually
- |> a BAD idea. But you DO have to include <stdlib.h> in any source file
- |> which calls malloc.
-
- Hmmm, and what should I do with this (especially when I have to use these
- compilers) ? :
-
- sun5:/users/peinel/clang(36)> uname -a
- SunOS sun5 4.1.3 3 sun4c
- sun5:/users/peinel/clang(35)> cat oh.c
- #include <stdlib.h>
-
- typedef struct item {
- int val;
- struct item *next;
- } ITEM, *PITEM;
-
- main()
- {
- PITEM head, current;
- head= malloc(sizeof(ITEM));
- head->val=1;
- }
- sun5:/users/peinel/clang(29)> cc oh.c
- "oh.c", line 11: warning: illegal pointer combination
- sun5:/users/peinel/clang(34)> which cc
- /bin/cc
- sun5:/users/peinel/clang(32)> CC oh.c
- CC mist.c:
- "oh.c", line 11: error: no standard conversion of char * to struct item *
- "oh.c", line 10: warning: current not used
- 1 error
- sun5:/users/peinel/clang(33)> which CC
- /com/owc++/bin/CC
-
- Without casting ?
-
- `gp'
-
- --
- gertraud peinel / ____) | | ulm /
- peinel@faw.uni-ulm.de / / / | | /
- / ___) /__ | | / | /
- _/_/ _/ _| _/ __/
-